package stack_queue.leetcode.queue.imp;

import stack_queue.leetcode.queue.Queue;

/**
 * @author shkstart
 * @create 2022-10-12-21:16
 */
public class LooQueue implements Queue {

    private int[] data;
    //有效元素的个数
    private int size;
    //队首元素下标
    private int front;
    //队尾元素的下一位置的下标
    private int tail;

    public LooQueue(int k) {
        data = new int[k+1];
    }

    @Override
    public void offer(int value) {
        //判断当前是否已满
        if(isFull()){
            System.out.println("queue  is full");
            return ;
        }
        data[tail] = value;
        tail = (tail+1)%data.length;
        size++;
    }

    @Override
    public int poll() {
        //判断当前是否为空
        if(isEmpty()){
            System.out.println("queue is empty");
            return -1;
        }
        int value = data[front];
        front = (front+1)%data.length;
        size--;
        return value;
    }

    @Override
    public int peek() {
        if(isEmpty()){
            System.out.println("queue is empty");
            return -1;
        }
        return data[front];
    }
    //如何查看队尾元素
    public int getTail(){
        if(isEmpty()){
            System.out.println("queue is empty");
            return -1;
        }
        //最后一个元素的下标
        int index = tail==0?data.length-1:tail-1;
        return data[index];

    }

    public boolean isFull(){
        if((tail+1)%data.length==front){
            return true;
        }
        return false;
    }
    public boolean isEmpty(){
        return tail == front;
    }

    public int getSize(){
        return size;
    }
    //遍历
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        //最后一个元素的索引
        int lastIndex = tail==0?data.length-1:tail-1;
        sb.append("front [ ");
//        遍历循环队列
        for(int i =front;i!=tail;){
            sb.append(data[i]);
            if(i!=lastIndex){
                sb.append(",");
            }
            i = (i+1)%data.length;
        }
        sb.append("] tail ");
        return sb.toString();
    }



}
